package repository;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author SHUtami
 */

//imported class
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class PenggunaController {
    public ResultSet AmbilPengguna(String Username){
        try {
            DatabaseController DBCon = new DatabaseController();
            DBCon.ConnecttoDB();
            Statement stmt = null;
            ResultSet rs = null;
            String SQL = "SELECT * FROM PENGGUNA WHERE username ='" + Username+"'";
            //System.out.print(SQL);
            stmt = DBCon.konek.createStatement();
            rs = stmt.executeQuery(SQL);
            return rs;
        } catch (SQLException ex) {
            Logger.getLogger(PenggunaController.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public Boolean isUserExist(String Username) throws SQLException {
        ResultSet all = AllPengguna();
        while (all.next()) {
            if (all.getString("username").equalsIgnoreCase(Username)) {
                return true;
            }
        }
        return false;
    }

    //Ambil seluruh pengguna
    public ResultSet AllPengguna(){
        try {
            DatabaseController DBCon = new DatabaseController();
            DBCon.ConnecttoDB();
            Statement stmt = null;
            ResultSet rs = null;
            String SQL = "SELECT * FROM PENGGUNA";
            //System.out.print(SQL);
            stmt = DBCon.konek.createStatement();
            rs = stmt.executeQuery(SQL);
            return rs;
        } catch (SQLException ex) {
            Logger.getLogger(PenggunaController.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public void InsertIntoPengguna(String Username,String Password, String Nama, String Email)
    {
        try {
            DatabaseController DBCon = new DatabaseController();
            DBCon.ConnecttoDB();
            Statement stmt = null;
            int rs = 0;
            String SQL = "INSERT INTO PENGGUNA(username, password, email) VALUES ("+
                    "'"+Username+"',"+
                    "'"+Password+"',"+
                    "'"+Email+"'";
            //System.out.print(SQL);
            stmt = DBCon.konek.createStatement();
            rs = stmt.executeUpdate(SQL);
        } catch (SQLException ex) {
            Logger.getLogger(PenggunaController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
